//
//  checkBox.m
//  whopaidforit
//
//  Created by Le Dung on 2/2/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "checkBox.h"
#import "LocationClass.h"

@implementation CheckBox
@synthesize keyArray,delegate;
-(void)drawCheckBox :(UIView *)view andLocation:(NSMutableArray*)dataArray;
{
    keyArray =[[NSMutableDictionary alloc]init];
    LocationClass *unit;
    for(int i = 0 ; i<dataArray.count; i++)
    {
        unit=[dataArray objectAtIndex:i];
        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame= CGRectMake(unit.xLocation, unit.yLocation, 18, 18);
        [button.layer setMasksToBounds:YES];
        button.layer.borderColor = [UIColor colorWithRed:0.278 green:0.275 blue:0.275 alpha:1.0].CGColor;
        button.layer.borderWidth = 1.0f;
        button.layer.cornerRadius = 10;
        button.layer.shadowOpacity = 1;
        [button.layer setShadowOffset:CGSizeMake(-0.2, -0.2)];
        [button.layer setShadowRadius:3];
        button.tag =i;
        [button addTarget:self action:@selector(acceptPress:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        [button setBackgroundColor:[UIColor clearColor]];
        
        [keyArray setObject:[NSNumber numberWithInt:0] forKey:[NSNumber numberWithInt:i]];
         }
}

-(IBAction)acceptPress:(id)sender
{
    UIButton *button=(UIButton *)sender;
    NSInteger value=[[keyArray objectForKey:[NSNumber numberWithInt:button.tag]]integerValue ];
    if(value)
    {
        [keyArray removeObjectForKey:[NSNumber numberWithInt:button.tag]];
        [keyArray setObject:[NSNumber numberWithInt:0] forKey:[NSNumber numberWithInt:button.tag]];
        [button setBackgroundColor:[UIColor clearColor]];
        
        int count=0;
        for(int i = 0 ; i<keyArray.count;i++)
            {
                if([[keyArray objectForKey:[NSNumber numberWithInt:i ]]integerValue ])
                 {
                    count++;
                 }
            }
        [[self delegate]checkBoxReply:button.tag andCount:count];
    }
    else
    {
        [keyArray removeObjectForKey:[NSNumber numberWithInt:button.tag]];
        [keyArray setObject:[NSNumber numberWithInt:1] forKey:[NSNumber numberWithInt:button.tag]];
        [button setBackgroundColor:[UIColor redColor]];
        int count=0;
        for(int i = 0 ; i<keyArray.count;i++)
        {
            if([[keyArray objectForKey:[NSNumber numberWithInt:i ]]integerValue ])
            {
                count++;
            }
        }
        [[self delegate]checkBoxReply:button.tag andCount:count];
        [[self delegate]keylistCheck:keyArray];
    }
}
@end
